package com.powerbank.user.controller;

import com.powerbank.common.result.Result;
import com.powerbank.user.dto.NearbyDeviceQueryDTO;
import com.powerbank.user.service.DeviceQueryService;
import com.powerbank.user.vo.DeviceInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import java.util.List;

/**
 * 设备查询控制器
 */
@Slf4j
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceQueryService deviceQueryService;

    /**
     * 查询附近设备
     */
    @PostMapping("/nearby")
    public Result<List<DeviceInfoVO>> findNearbyDevices(@RequestBody @Valid NearbyDeviceQueryDTO queryDTO) {
        List<DeviceInfoVO> devices = deviceQueryService.findNearbyDevices(queryDTO);
        return Result.success(devices);
    }

    /**
     * 获取设备详情
     */
    @GetMapping("/detail/{deviceCode}")
    public Result<DeviceInfoVO> getDeviceDetail(@PathVariable @NotBlank(message = "设备编码不能为空") String deviceCode) {
        DeviceInfoVO device = deviceQueryService.getDeviceDetail(deviceCode);
        return Result.success(device);
    }

    /**
     * 检查设备是否可用
     */
    @GetMapping("/available/{deviceCode}")
    public Result<Boolean> checkDeviceAvailable(@PathVariable @NotBlank(message = "设备编码不能为空") String deviceCode) {
        boolean available = deviceQueryService.isDeviceAvailable(deviceCode);
        return Result.success(available);
    }
}